package com.lxk.util.exception;

import com.lxk.vo.result.CodeMsg;
import com.lxk.vo.result.Result;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author JohnSoybean
 * @create 2020/3/19 16:06
 * @description 全局异常处理
 */
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    @ExceptionHandler(value=Exception.class)
    public Object exceptionHandler(HttpServletRequest request, Exception e){
        e.printStackTrace();
        //判断是否为Ajax请求
        if (isAjax(request)) {
            if(e instanceof GlobalException) {
                GlobalException ex = (GlobalException)e;
                return Result.error(ex.getCm());
            }else if(e instanceof BindException) {
                BindException ex = (BindException)e;
                List<ObjectError> errors = ex.getAllErrors();
                ObjectError error = errors.get(0);
                String msg = error.getDefaultMessage();
                return Result.error(CodeMsg.BIND_ERROR.fillArgs(msg));
            }else if(e instanceof NullPointerException) {
                return Result.error(CodeMsg.NULL_POINTER);
            }else if(e instanceof NoHandlerFoundException){
                return Result.error(CodeMsg.NO_HANDLER_FOUND);
            }else {
                return Result.error(CodeMsg.SERVER_ERROR);
            }
        }else{
            ModelAndView modelAndView = new ModelAndView("errorPage");
            if(e instanceof GlobalException) {
                GlobalException ex = (GlobalException)e;
                modelAndView.addObject("result", Result.error(ex.getCm()));
            }else if(e instanceof BindException) {
                BindException ex = (BindException)e;
                List<ObjectError> errors = ex.getAllErrors();
                ObjectError error = errors.get(0);
                String msg = error.getDefaultMessage();
                modelAndView.addObject("result", Result.error(CodeMsg.BIND_ERROR.fillArgs(msg)));
            }else if(e instanceof NullPointerException) {
                modelAndView.addObject("result", Result.error(CodeMsg.NULL_POINTER));
            }else if(e instanceof NoHandlerFoundException){
                modelAndView.addObject("result", Result.error(CodeMsg.NO_HANDLER_FOUND));
            }else {
                modelAndView.addObject("result", Result.error(CodeMsg.SERVER_ERROR));
            }
            return modelAndView;
        }

    }


    /**
     * 判断是否为Ajax请求
     * @param request
     * @return
     */
    public boolean isAjax(HttpServletRequest request) {
        return (request.getHeader("X-Requested-With") != null &&
                "XMLHttpRequest".equals(request.getHeader("X-Requested-With").toString()));
    }

}
